import java.io.*;
import java.net.*;
import java.util.*;



public class NS9_userB{
    
    public static int find_pow(int a, int b,int c) {
        int i,final_ans=1;
        for(i=0;i<b;i++)
            final_ans=(final_ans*(a%c))%c;
        return final_ans%c;
    }
    
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter value of P");
        int P = sc.nextInt();
        
        System.out.println("Enter value of a");
        int a = sc.nextInt();

        
        System.out.println("Enter value of Xb");
        int Xb = sc.nextInt();
        
        int Yb;
        Yb=find_pow(a,Xb,P);

        
        Socket s = null;
        s = new Socket("127.0.0.1",7777);
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        DataInputStream dis = new DataInputStream(s.getInputStream());
        
        String Ya = dis.readUTF();
        dos.writeUTF(Integer.toString(Yb));

        int k;
        k=find_pow(Integer.parseInt(Ya),Xb,P);
            
        System.out.println("Key is "+k);
    }
    
}